/* eslint-disable no-param-reassign */
import taskApi from '@/api/modules/task'
import i18n from '@/lang'
const initQueryParams = {
  name: '',
  strategyId: undefined,
  vehicleVersion: undefined, // 车辆版本
  enableTimeGte: null, // 启用时间起
  enableTimeLte: null, // 启用时间止
  createTimeGte: null, // 创建时间起
  createTimeLte: null, // 创建时间止
  // 活跃度查询
  lastActive: undefined,
  // 最近升级
  lastUpgradeSuccess: null,
  // 按什么排序,值是排序字段名:排序方式  如order=StartTime:ASC StartTime:DESC
  order: null,
  type: null,
  silenceUpgrade: undefined,
  forceUpgrade: undefined,
  modelId: undefined
}
const state = {
  // 是否自动升级 1:用户升级 2：自动升级
  taskType: [{
    //紧急OTA都是自动升级
    id: 1,
    name: 'usersj',
    value: '1'
  }, {
    id: 2,
    name: 'zdsj',
    value: '2'
  }],
  // 是否静默升级 1静默 0非静默
  slienceUpgradeType: [{
    id: '1',
    name: 'jmsj',
    value: true
  }, {
    id: '0',
    name: 'fjmsj',
    value: false
  }],
  // 是否强制升级 1 强制升级 2非强制升级
  forceUpgradeType: [{
    id: '1',
    name: 'qzsj',
    value: true
  }, {
    id: '0',
    name: 'fqzsj',
    value: false
  }],
  // 是否自由烧写
  freeBurningType: [{
    id: '1',
    name: 'zysx',
    value: 1
  }, {
    id: '0',
    name: 'fzysx',
    value: 0
  }],
  // 是否自动升级 9:用户升级 8：自动升级 10循环升级
  testTaskType: [{
    id: 9,
    name: 'usersj',
    value: '9'
  }, {
    id: 8,
    name: 'zdsj',
    value: '8'
  }, {
    id: 10,
    name: 'circleUpdate',
    value: '10'
  }],
  // 进阶条件
  conditionType: [
    {
      name: 'qie',
      value: 1
    }, {
      name: 'huo',
      value: 2
    }
  ],
  // 任务操作状态
  operationStatus: [
    { name: 'normal', id: 1, value: 1 },
    { name: 'paused', id: 2, value: 2 },
    { name: 'canceled', id: 3, value: 3 }
  ],
  queryParams: { ...initQueryParams }
}

const mutations = {
  // 清空查询条件，还原到默认设置的条件
  resetTaskQueryParams (state) {
    state.queryParams = { ...initQueryParams }
  }
}

const getters = {
  getTaskDictCodeObj: state => dictCodeKey => {
    const obj = {}
    state[dictCodeKey].forEach(item => {
      obj[item.value] = i18n.t(item.name)
    })
    return obj
  }
}

const actions = {
  // 添加
  // eslint-disable-next-line no-unused-vars
  // addTaskData ({ commit }, data) {
  //   return new Promise((resolve, reject) => {
  //     taskApi.addTaskData(data).then(res => {
  //       resolve(res)
  //     }).catch(err => {
  //       reject(err)
  //     })
  //   })
  // },
  // 获取任务分页数据
  // eslint-disable-next-line no-unused-vars
  getTaskPageData ({ commit }, query) {
    return new Promise((resolve, reject) => {
      taskApi.getTaskPageData(query).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 获取某任务下车辆列表
  // eslint-disable-next-line no-unused-vars
  getTaskVehiclePageData ({ commit }, query) {
    return new Promise((resolve, reject) => {
      taskApi.getTaskVehiclePageData(query).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 获取任务下拉数据
  // eslint-disable-next-line no-unused-vars
  getTaskSelectData ({ commit }, query) {
    return new Promise((resolve, reject) => {
      taskApi.getTaskSelect(query).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  }
}
export default {
  state,
  mutations,
  actions,
  getters
}
